/**
 * Copyright [2010] Delta Air Lines, Inc. All Rights Reserved Access,
 * Modification, or Use Prohibited Without Express Permission of Delta Air
 * Lines.
 */
package com.delta.rafg.core.database;

import com.delta.rafg.core.config.ConfigSettings;
import com.delta.rafg.core.database.impl.ACacheDataHandler;
import com.delta.rafg.core.database.impl.ADataHandler;
import com.delta.rafg.core.database.impl.GenericDataHandlerImpl;
import com.delta.rafg.core.database.impl.JavaDBCacheDataHandlerImpl;
import com.delta.rafg.core.database.impl.JavaDBSeedDataHandlerImpl;
import com.delta.rafg.core.database.impl.ReferenceDataHandlerImpl;
import com.delta.rafg.core.exception.RAFGException;

/**
 * @author RAFG Development Team (D9127520)
 *
 */
public final class DataHandlerFactory {

    /**
     *
     */
    private DataHandlerFactory() {
    }

    /**
     * @return Instance of Seed File DataHandler Implementation.
     */
    public static IDataHandler getNewSeedDataHandler() {
        return new JavaDBSeedDataHandlerImpl();
    }

    /**
     * @return Instance of Reference/Config Data Cache DataHandler Implementation.
     */
    public static ACacheDataHandler getNewCacheDataHandler() {
        return new JavaDBCacheDataHandlerImpl();
    }

    /**
     * @return Instance of Reference Data DataHandler Implementation.
     */
    public static IDataHandler getNewReferenceDataHandler() {
        return new ReferenceDataHandlerImpl();
    }
    
    /**
     * Return singleton instance of cache data handler
     * @return ACacheDataHandler
     * @throws RAFGException
     */
    public static ACacheDataHandler getCacheDataHandler() throws RAFGException {
    	return (ACacheDataHandler) CacheController.asSingleton().getCacheDataHandler();
    }

    /** 
     * Return singleton instance of a specific reference data handler
     * @param identifier
     * @return
     * @throws RAFGException 
     */
    public static IDataHandler getReferenceDataHandler(String identifier) throws RAFGException {
    	return CacheController.asSingleton().getReferenceDataHandler(identifier);
    }

    /** 
     * Return singleton instance of first (default) reference data handler
     * @return
     * @throws RAFGException 
     */
    public static IDataHandler getReferenceDataHandler() throws RAFGException {
    	return CacheController.asSingleton().getReferenceDataHandler();
    }

    public static IDataHandler getNewGenericDataHandler(String genericDataSource) throws RAFGException {
    	IGenericDataSource genericDataSourceClass = null;
    	try {
    		genericDataSourceClass = (IGenericDataSource) Class.forName(genericDataSource).newInstance();
    		return getNewGenericDataHandler(genericDataSourceClass);
        } catch (IllegalAccessException e) {
            throw new RAFGException(e);
        } catch (InstantiationException e) {
            throw new RAFGException(e);
        } catch (ClassNotFoundException e) {
            throw new RAFGException(e);
        }
    }

    public static IDataHandler getNewGenericDataHandler(IGenericDataSource genericDataSource) throws RAFGException {
    	return new GenericDataHandlerImpl(genericDataSource);
    }
    
    /**
     * @param identifier Data handler identifier
     * @return IDataHandler
     * @throws RAFGException RAFGException
     */
    public static IDataHandler getNewDataHandlerImpl(final String identifier) throws RAFGException {
        IDataHandler handler = null;
        try {
            ConfigSettings props = ConfigSettings.getSingleton();
            String handlerClassName =
                props.getProperty(ADataHandler.PROP_DATASOURCE_CONFIG_PREFIX + "." + identifier + ".datahandler");
            handler = (IDataHandler) Class.forName(handlerClassName).newInstance();
        } catch (IllegalAccessException e) {
            throw new RAFGException(e);
        } catch (InstantiationException e) {
            throw new RAFGException(e);
        } catch (ClassNotFoundException e) {
            throw new RAFGException(e);
        }
        return handler;
    }
}
